package com.yang.studentsapp.ui.friends;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.yang.studentsapp.R;
import com.yang.studentsapp.ui.adapter.FragmentAdapter;
import com.yang.studentsapp.ui.message.FastmsgFragment;
import com.yang.studentsapp.utils.FindViewUtils;
import com.yang.studentsapp.utils.ResUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${Ян-Вэнь-Чао} on 2016/10/10.
 * 朋友
 */
public class FriendsFragemtn extends Fragment {
    ViewPager viewPager;
    RadioGroup radioGroup;
    RadioButton radioFrig, radioCluster;
    ImageView imageView;
    PopupWindow popupWindow;
    ListView listView;
    List<Fragment> list = new ArrayList<>();
    FragmentAdapter adapter;

    FriendsFragment friendsFragment;
    GroupFragment groupFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_friend, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        viewpager();

        imageView = (ImageView) getActivity().findViewById(R.id.friment_add);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopuw(v);
            }
        });
    }
    private void viewpager() {
        friendsFragment = new FriendsFragment();
        groupFragment = new GroupFragment();
        list.add(friendsFragment);
        list.add(groupFragment);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.frigment_frig:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.frigment_cluster:
                        viewPager.setCurrentItem(1);
                        break;
                }
                radioFrig.isChecked();
            }
        });
        adapter = new FragmentAdapter(getChildFragmentManager(), list);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        radioFrig.setChecked(true);
                        break;
                    case 1:
                        radioCluster.setChecked(true);
                        break;
                }
                radioFrig.isChecked();
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
    private void initView() {
        viewPager = (ViewPager) getActivity().findViewById(R.id.frigment_viewpager);
        radioGroup = (RadioGroup) getActivity().findViewById(R.id.frigment_group);
        radioFrig = (RadioButton) getActivity().findViewById(R.id.frigment_frig);
        radioCluster = (RadioButton) getActivity().findViewById(R.id.frigment_cluster);

    }
    private void showPopuw(View v) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.popuwindow, null, false);
        listView = (ListView) view.findViewById(R.id.popu_list);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                return false;
            }
        });
        if (radioFrig.isChecked()) {
           listData1();
            setListener1();
         popupWindow = new PopupWindow(view, 180, 200);

        } else {
            listdata();
            setListener();
         popupWindow = new PopupWindow(view, 180, 150);

        }
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(String.valueOf(ResUtils.getColor(R.color.bai))));
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }
        });

        popupWindow.showAsDropDown(v,10, 10);

    }

    private void listdata() {
        List<Map<String, Object>> list = new ArrayList<>();

        String[] name = new String[]{"创建群组","添加群组"};
        int[] icon = new int[]{R.drawable.creation,R.drawable.add};
        for (int i = 0; i < name.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", name[i]);
            map.put("icon", icon[i]);
            list.add(map);
        }
        String[] from = {"name", "icon"};
        final int[] to = {R.id.popu_add_text, R.id.popu_add_image};
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.popu_item,
                from, to);
        if (listView != null) {
            listView.setAdapter(adapter);
            Log.v("tag", "listView添加陈功");
        }
    }

    private void listData1() {
        List<Map<String, Object>> list = new ArrayList<>();

        String[] name = new String[]{"添加好友","摇一摇","创建圈子"};
        int[] icon = new int[]{R.drawable.add_buddy,R.drawable.shake,R.drawable.create_circle};
        for (int i = 0; i < name.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", name[i]);
            map.put("icon", icon[i]);
            list.add(map);
        }
        String[] from = {"name", "icon"};
        final int[] to = {R.id.popu_add_text, R.id.popu_add_image};
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.popu_item,
                from, to);
        if (listView != null) {
            listView.setAdapter(adapter);
            Log.v("tag", "listView添加陈功");
        }
    }

    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Toast.makeText(getContext(), "急急急", Toast.LENGTH_SHORT).show();
                        FragmentManager manager = getActivity().getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        transaction.add(android.R.id.content,new CreateGroupFragment());
                        transaction.addToBackStack(null);
                        transaction.commit();
                        popupWindow.dismiss();
                        break;
                    case 1:
                        Toast.makeText(getContext(),"急苏打水急",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void setListener1() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentManager manager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();

                switch (position) {
                    case 0:
                        Toast.makeText(getContext(), "华盛顿峰会将贷款", Toast.LENGTH_SHORT).show();
                        transaction.replace(android.R.id.content, new AddFriendsFragment());
                        transaction.addToBackStack(null);
                        transaction.commit();
                        break;
                    case 1:
                        Toast.makeText(getContext(), "将贷款", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(getContext(), "华盛将贷款", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
}
